<template>
    <component
      is="a-modal"
      v-model:visible="visible"
      :width="600"
      :title="title"
      :mask-closable="false"
      :ok-loading="loading"
      @cancel="close"
      @before-ok="submit">
      <!-- 表单信息 start -->
      <a-form ref="formRef" :model="formData" :rules="rules" :auto-label-width="true">
        <a-row :gutter="10">
          <a-col :span="24">
            <a-form-item label="上级菜单" field="parent_id">
              <a-tree-select
                v-model="formData.parent_id"
                :data="optionData.treeData"
                :field-names="{ key: 'id', title: 'menu_name', icon: 'customIcon' }"
                placeholder="请选择上级菜单"
                allow-clear />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="菜单名称" field="menu_name">
              <a-input v-model="formData.menu_name" placeholder="请输入菜单名称" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="图标" field="icon">
              <tn-icon v-model="formData.icon"></tn-icon>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="颜色" field="color">
              <ma-color-picker v-model="formData.color" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="是否链接" field="is_href">
              <sa-radio v-model="formData.is_href" dict="yes_or_no" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="页面地址" field="page_url" v-if="formData.is_href == 1">
              <a-input v-model="formData.page_url" placeholder="请输入页面地址" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="排序" field="sort">
              <a-input-number v-model="formData.sort" placeholder="请输入排序" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="状态" field="status">
              <sa-radio v-model="formData.status" dict="data_status" />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
      <!-- 表单信息 end -->
    </component>
  </template>
  
  <script setup>
  import { ref, reactive, computed } from 'vue'
  import { Message, Modal } from '@arco-design/web-vue'
  import commonApi from '@/api/common'
  import api from '../../api/app/menu'
  import tnIcon from '../../components/tn-icon/index.vue'
  
  const emit = defineEmits(['success'])
  // 引用定义
  const visible = ref(false)
  const loading = ref(false)
  const formRef = ref()
  const mode = ref('')
  const optionData = reactive({
    treeData: [],
  })
  
  let title = computed(() => {
    return '工作中心' + (mode.value == 'add' ? '-新增' : '-编辑')
  })
  
  // 表单信息
  const formData = reactive({
    id: null,
    parent_id: null,
    menu_name: '',
    is_href: 2,
    sort: 100,
    icon: '',
    color: '',
    page_url: '',
    status: 1,
  })
  
  // 验证规则
  const rules = {
    menu_name: [{ required: true, message: '菜单名称必需填写' }],
  }
  
  // 打开弹框
  const open = async (type = 'add') => {
    mode.value = type
    visible.value = true
    formRef.value.resetFields()
    await initPage()
  }
  
  // 初始化页面数据
  const initPage = async () => {
    const resp = await api.getPageList()
    optionData.treeData = resp.data
  }
  
  // 设置数据
  const setFormData = async (data) => {
    for (const key in formData) {
      if (data[key] != null && data[key] != undefined) {
        formData[key] = data[key]
      }
    }
  }
  
  // 数据保存
  const submit = async (done) => {
    const validate = await formRef.value?.validate()
    if (!validate) {
      loading.value = true
      let data = { ...formData }
      let result = {}
      if (mode.value === 'add') {
        // 添加数据
        data.id = undefined
        result = await api.save(data)
      } else {
        // 修改数据
        result = await api.update(data.id, data)
      }
      if (result.code === 200) {
        Message.success('操作成功')
        emit('success')
        done(true)
      }
      // 防止连续点击提交
      setTimeout(() => {
        loading.value = false
      }, 500)
    }
    done(false)
  }
  
  // 关闭弹窗
  const close = () => (visible.value = false)
  
  defineExpose({ open, setFormData })
  </script>
  